package com.punai.config.controller;

import com.punai.common.core.controller.BaseController;
import com.punai.common.core.domain.AjaxResult;
import com.punai.common.core.domain.TreeBo;
import com.punai.config.constants.PnConfigConstants;
import com.punai.config.domain.ConfigFormatCategory;
import com.punai.config.service.ConfigFormatCategoryService;
import com.punai.config.service.ConfigGenerateCodeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.util.List;

@RestController
@RequestMapping("/config/formatCategory")
public class ConfigFormatCategoryController extends BaseController {

    @Autowired
    private ConfigFormatCategoryService configFormatCategoryService;
    @Autowired
    private ConfigGenerateCodeService configGenerateCodeService;

    @PreAuthorize("@ss.hasPermi('config:formatCategory:list')")
    @GetMapping("treelist")
    public AjaxResult treelist() {
        List<TreeBo> treeBos = configFormatCategoryService.treelist();
        return AjaxResult.success(treeBos);
    }

    @PreAuthorize("@ss.hasPermi('config:formatCategory:addedit')")
    @PostMapping("saveOrUpdate")
    public AjaxResult saveOrUpdate(@RequestBody ConfigFormatCategory configFormatCategory) throws ParseException {
        return toAjax(configFormatCategoryService.saveOrUpdateFormatCategory(configFormatCategory));
    }

    @PreAuthorize("@ss.hasPermi('config:formatCategory:query')")
    @GetMapping("/getInfo/{id}")
    public AjaxResult getInfo(@PathVariable String id) {
        return AjaxResult.success(configFormatCategoryService.getInfo(id));
    }

    @PreAuthorize("@ss.hasPermi('config:formatCategory:remove')")
    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable String id) {
        return toAjax(configFormatCategoryService.delete(id));
    }

    @PreAuthorize("@ss.hasPermi('config:formatCategory:query')")
    @GetMapping("getNextCode")
    public AjaxResult getNextCode() {
        return AjaxResult.success("操作成功",configGenerateCodeService.getNextCode(PnConfigConstants.CODE_TYPE.YT_CODE));
    }

    @PreAuthorize("@ss.hasPermi('config:formatCategory:sync')")
    @PutMapping("syncFormatCategoryCode")
    public AjaxResult syncFormatCategory() {
        return AjaxResult.success(configGenerateCodeService.syncSqliteMaxCodeToDb(PnConfigConstants.CODE_TYPE.YT_CODE));
    }
}
